package com.deyuanyun.pic.settings.service.msgtemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.settings.controller.dto.BizMsgThresholdVO;
import com.deyuanyun.pic.settings.controller.dto.base.AcDcMiorRecordDTO;
import com.deyuanyun.pic.settings.controller.dto.base.DefectInfoDTO;
import com.deyuanyun.pic.settings.controller.dto.base.ExinspCptestStationDTO;
import com.deyuanyun.pic.settings.controller.dto.base.FeatureTypeDTO;
import com.deyuanyun.pic.settings.controller.dto.base.RiskAssessmentDTO;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.domain.base.BizMsgUser;
import com.deyuanyun.pic.settings.enumeration.ThresholdType;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;
import com.deyuanyun.pic.settings.support.utils.BaseCodeUtils;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *  管道周期提醒消息推送
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-08-08
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class RiskSystemPushMsg extends AbstractPushMsgTemplate {

    private static final Logger LOGGER = LoggerFactory.getLogger(RiskSystemPushMsg.class);

    /** 风险等级key */
    public static final String RISK_GRADE_KEY = "riskGrade";
    /** 地区等级key */
    public static final String AREA_LEVEL_KEY = "areaLevel";
    /** 角色id key */
    public static final String ROLE_ID_KEY = "roleId";
    /** 统一编号key */
    public static final String CODE_LIST_KEY = "codeList";
    /** 创建备注 */
    private String createRemark = "系统风险等级消息推送";
    /** 修改备注 */
    private String updateRemark = "更新时间用于重复消息推送判断关键字段,用户风险评价数据录入后";


    @Override
    public void doPushMsgData() {
        if(super.interfaceParam.isEmpty()) {
            LOGGER.info("没有传入参数");
            return;
        }
        BizMsgThresholdVO bizMsgThresholdVO = super.bizMsgThresholdService.queryDataByTypeOne(ThresholdType.RISK_SYSTEM_SCHEDULE.getValue());
        if(LOGGER.isInfoEnabled()) {
            LOGGER.info("获取到系统风险等级设置信息为：" + bizMsgThresholdVO.getThresholdContent());
        }
        List<JSONObject> jsonObjects = JSONArray.parseArray(bizMsgThresholdVO.getThresholdContent(), JSONObject.class);

        Collection<RiskAssessmentDTO> satisfyCollections = null; //声明一个接口或非接口执行的时候推送满足条件的风险管理信息集合

        Collection<RiskAssessmentDTO> interfaceColl = null; //接口调用的时候根据统一编号查询到的风险管理信息集合

        if(super.isInterface) {//当是接口调用的时候把统一编号集合放入条件map中到数据库查询数据
            Object param = interfaceParam.get(CODE_LIST_KEY);
            if(param != null && param instanceof List) {
                if(LOGGER.isInfoEnabled()) {
                    LOGGER.info("当前操作为风险管理数据录入后，对应的统一编号有"+ ((List)param).size() + "条");
                }
                //满足条件的风险管理信息
                interfaceColl = super.bizMsgThresholdService.queryRiskAssessmentInfo(interfaceParam);

                LOGGER.info("根据统一编号查询到：" + interfaceColl.size() + "条满足初步推送消息的记录");
            }
        }
        for (JSONObject jsonObject : jsonObjects) {
            final int riskGrade = jsonObject.getIntValue(RISK_GRADE_KEY);//风险等级
            final int areaLevel = jsonObject.getIntValue(AREA_LEVEL_KEY);//地区等级
            if(super.isInterface) {//当是接口调用的时候，用java代码筛选出需要推送的信息集合
                satisfyCollections = Collections2.filter(interfaceColl, new Predicate<RiskAssessmentDTO>() {
                    @Override
                    public boolean apply(@Nullable RiskAssessmentDTO input) {
                        if (input != null) {
                            if (input.getRiskIntGrade() == riskGrade && input.getAreaIntLevel() >= areaLevel) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                        return false;
                    }
                });
            } else { //不是接口调用的时候，带着地区等级、风险等级去数据库查询需要推送的信息集合
                interfaceParam.clear();
                interfaceParam.put(RISK_GRADE_KEY, riskGrade);
                interfaceParam.put(AREA_LEVEL_KEY, areaLevel);
                satisfyCollections = super.bizMsgThresholdService.queryRiskAssessmentInfo(interfaceParam);
            }
            String roleIdStr = jsonObject.getString(ROLE_ID_KEY);//角色id逗号分割的id
            if(LOGGER.isInfoEnabled()) {
                LOGGER.info("得到当前的地址等级为：" + areaLevel + "，风险等级为：" + riskGrade + "，提醒的角色为：" + roleIdStr);
            }
            if(StringUtils.isNotEmpty(roleIdStr)) { //把角色id封装成角色id集合
                super.roleList = Arrays.asList(roleIdStr.split(","));
            }
            if(LOGGER.isInfoEnabled()) {
                LOGGER.info("总共查询到：" + satisfyCollections.size() + "条满足初步推送消息的记录");
            }
            for (RiskAssessmentDTO riskAssessment : satisfyCollections) {
                Date lastDate = getLastDate(riskAssessment.getCreateDate(), riskAssessment.getUpdateDate());
                setThresholdContent(new Corrosion().setPipeLineId(riskAssessment.getPipeLineId()).setPipeLineName(riskAssessment.getPipeLineName())
                        .setFromId(riskAssessment.getFromReferenceId()).setToId(riskAssessment.getToReferenceId()).setOverUnderStr(""));
                initializeBizMsgSends(lastDate, bizMsgThresholdVO, true, riskAssessment.getCode());
            }
        }
        if (super.isInterface) { //当是接口调用的时候
            if(LOGGER.isInfoEnabled()) {
                LOGGER.info("开始处理风险评价数据录入后的主要威胁因素消息推送");
            }
            initializeMenaceBizSends(interfaceColl);
            if(LOGGER.isInfoEnabled()) {
                LOGGER.info("处理风险评价数据录入后的主要威胁因素消息推送结束");
            }
        }
    }

    /**
     * 设置阀值信息
     * @param corrosion
     */
    private void setThresholdContent(Corrosion corrosion) {
        String pipeLineId = corrosion.getPipeLineId();
        PipeLocationService pipeLocationService = SpringAppliction.getBean(PipeLocationService.class);
        PipeLocationDTO locationFrom = pipeLocationService.queryPipeLocationById(pipeLineId, corrosion.getFromId());
        PipeLocationDTO locationTo = pipeLocationService.queryPipeLocationById(pipeLineId, corrosion.getToId());
        if (locationFrom != null && locationTo != null) {
            List list = Lists.newArrayList(corrosion.getPipeLineName(), locationFrom.getName(), locationTo.getName());
            if(corrosion.isThirdDamage()) { //当威胁因素为第三方破坏的时候
                List l = new ArrayList(list);
                list.addAll(l);
            }
            String overUnderStr = corrosion.getOverUnderStr();
            if(StringUtils.isNotEmpty(overUnderStr)) {
                list.add(overUnderStr);
            }
            String acDcStr = corrosion.getAcDcStr();
            if(StringUtils.isNotEmpty(acDcStr)) {
                list.add(acDcStr);
            }
            super.thresListCastString(list);
        }
    }

    /**
     * 根据更新时间和创建时间来返回最终需要的时间
     * @param createDate 创建时间
     * @param updateDate 更新时间
     * @return
     */
    private Date getLastDate(Date createDate, Date updateDate) {
        if(createDate == null && updateDate == null) return new Date(); //当更新时间和创建时间都为空的时候直接返回当前时间
        if(createDate == null && updateDate != null) {
            return updateDate;
        } else if(createDate != null && updateDate == null) {
            return createDate;
        } else if (createDate != null && updateDate != null) {
            return updateDate;
        } else {//当if都不满足的时候直接返回当前时间
            return new Date();
        }
    }

    /**
     * 威胁因素消息推送
     * @param riskAssessmentDTOs
     */
    private void initializeMenaceBizSends(Collection<RiskAssessmentDTO> riskAssessmentDTOs) {
        List<BizMsgThresholdVO> bizMsgThresholdVOList = bizMsgThresholdService.queryChildForPage(ThresholdType.RISK_EVALUATION.getValue());

        Map<String, BizMsgThresholdVO> riskBizThresholdMap = new HashMap<String, BizMsgThresholdVO>();

        for (int i = 0; i < bizMsgThresholdVOList.size(); i++) {
            BizMsgThresholdVO msgThresholdVO = bizMsgThresholdVOList.get(i);
            riskBizThresholdMap.put(msgThresholdVO.getType(), msgThresholdVO);
        }
        
        BizMsgThresholdVO misoperation = riskBizThresholdMap.get(ThresholdType.RISK_MENACE_MISOPERATION.getValue());//误操作阀值信息
        BizMsgThresholdVO manufactureCons= riskBizThresholdMap.get(ThresholdType.RISK_MENACE_MANUFACTURE_CONSTRUCTION.getValue());//制造与施工缺陷阀值信息
        BizMsgThresholdVO geologicHazard = riskBizThresholdMap.get(ThresholdType.RISK_MENACE_GEOLOGIC_HAZARD.getValue()); //地质灾害阀值信息
        BizMsgThresholdVO damage = riskBizThresholdMap.get(ThresholdType.RISK_MENACE_THIRD_PARTY_DAMAGE.getValue());//第三方破坏阀值信息
        BizMsgThresholdVO immediateRepair = riskBizThresholdMap.get(ThresholdType.RISK_IMMEDIATE_REPAIR.getValue());//立即修复的管体缺陷
        BizMsgThresholdVO corrosionPercent = riskBizThresholdMap.get(ThresholdType.RISK_CORROSION_PERCENT.getValue());//内腐蚀占比
        BizMsgThresholdVO overUnderProtection = riskBizThresholdMap.get(ThresholdType.RISK_OVER_UNDER_PROTECTION.getValue());//过保护与欠保护
        BizMsgThresholdVO acDcMior = riskBizThresholdMap.get(ThresholdType.RISK_AC_DC_INTERFERENCE.getValue());//交流直流干扰

        for (RiskAssessmentDTO riskAssessmentDTO : riskAssessmentDTOs) {
            String lineId = riskAssessmentDTO.getPipeLineId();//管道id
            BigDecimal fromMileage = riskAssessmentDTO.getFromMileage(); //起点绝对距离
            BigDecimal toMileage = riskAssessmentDTO.getToMileage(); //终点绝对距离
            String pipeLineName = riskAssessmentDTO.getPipeLineName(); //管道名称
            String fromReferenceId = riskAssessmentDTO.getFromReferenceId(); //起点最近参考点编号
            String toReferenceId = riskAssessmentDTO.getToReferenceId(); //终点最近参考点编号

            Date lastDate = getLastDate(riskAssessmentDTO.getCreateDate(), riskAssessmentDTO.getUpdateDate());
            String code = riskAssessmentDTO.getCode(); //统一编号
            String mainThreats = riskAssessmentDTO.getMainThreats(); //威胁因素

            Corrosion corrosion = new Corrosion().setPipeLineName(lineId).setPipeLineName(pipeLineName)
                    .setFromId(fromReferenceId).setToId(toReferenceId).setOverUnderStr("");

            if("RA_MAIN_THREATS_FS".equals(mainThreats)) {//腐蚀
                //***立即修复的缺陷列表
                List<DefectInfoDTO> defectInfoDTOs = super.bizMsgThresholdIntefaceService.queryImmeRepairDefects(lineId, fromMileage, toMileage);
                if(defectInfoDTOs != null && !defectInfoDTOs.isEmpty()) {
                    super.roleList = immediateRepair.getRoleIdList();
                    this.createRemark = "立即修复管体缺陷消息推送";
                    setThresholdContent(corrosion);
                    initializeBizMsgSends(lastDate, immediateRepair, true, code);
                }

                //***内腐蚀占比
                float countCorrosion = 0;//内腐蚀总数
                float countAll = 0;//缺陷总数
                //内腐蚀占总缺陷百分比
                List<FeatureTypeDTO> countMaps = super.bizMsgThresholdIntefaceService.countFeatureType(lineId, fromMileage, toMileage);
                for (FeatureTypeDTO subtypeDTO : countMaps) {
                    String subType = subtypeDTO.getFeatureType();
                    int countFeature = subtypeDTO.getCountFeature();

                    if(subType == null) {
                        return;
                    }
                    countAll += countFeature;
                    subType = BaseCodeUtils.getMinorCode(subType);
                    //为内腐蚀时取出来它对应的数量
                    if("METALOSS_TYPE_NBJSSS".equals(subType) || "METALOSS_TYPE_WBJSSS".equals(subType) ||
                            "METALOSS_TYPE_JSSS".equals(subType)) {
                        countCorrosion += countFeature;
                    }
                }
                String corrosionPercentStr =  corrosionPercent.getThresholdContent();
                if(StringUtils.isNotEmpty(corrosionPercentStr)) {
                    JSONObject jsonObject = JSON.parseObject(corrosionPercentStr);
                    Float percent = jsonObject.getFloat("percent");
                    if (percent != null) {
                        if ((countCorrosion / countAll) > percent.floatValue() / 100) {// 内腐蚀占比大于阀值设置的信息则推送消息
                            super.roleList = Arrays.asList(corrosionPercent.getRoleId().split(","));
                            this.createRemark = "内腐蚀占比消息推送";
                            setThresholdContent(corrosion);
                            initializeBizMsgSends(null, corrosionPercent, false);
                        }
                    }
                }

                Long beginMile = fromMileage.multiply(new BigDecimal(1000)).longValue(); //起点绝对距离，整形
                Long endMile = toMileage.multiply(new BigDecimal(1000)).longValue(); //终点绝对距离，整形

                //***管道存在过保护欠保护
                Object  overUnderObj = overUnderProtection.getThresholdContentJson();
                if(overUnderObj != null && overUnderObj != "" && (overUnderObj instanceof Map)) {
                    try {
                        overUnderProtection(overUnderProtection, lineId, pipeLineName, fromReferenceId, toReferenceId, beginMile, endMile, (Map) overUnderObj);
                    } catch (Exception e) {
                        LOGGER.error("管道存在过保护欠保护消息推送异常", e);
                    }
                }

                //***交流/直流干扰
                Object acDcMiorObj = acDcMior.getThresholdContentJson();
                if(acDcMiorObj != null && acDcMiorObj != "" && (acDcMiorObj instanceof Map)) {
                    try {
                        acDcMior(acDcMior, lineId, pipeLineName, fromReferenceId, toReferenceId, beginMile, endMile, (Map) acDcMiorObj);
                    } catch (Exception e) {
                        LOGGER.error("交流/直流干扰消息推送异常", e);
                    }
                }

            } else if("RA_MAIN_THREATS_MIDDLE_WCZ".equals(mainThreats)) {//误操作
                super.roleList = misoperation.getRoleIdList();
                this.createRemark = "误操作消息推送";
                setThresholdContent(corrosion);
                initializeBizMsgSends(lastDate, misoperation, true, code);
            } else if("RA_MAIN_THREATS_ZZYSQX".equals(mainThreats)) {//制造与施工缺陷
                super.roleList = manufactureCons.getRoleIdList();
                this.createRemark = "制造与施工缺陷消息推送";
                setThresholdContent(corrosion);
                initializeBizMsgSends(lastDate, manufactureCons, true, code);
            } else if("RA_MAIN_THREATS_MIDDLE_DZZH".equals(mainThreats)) {//地质灾害
                super.roleList = geologicHazard.getRoleIdList();
                this.createRemark = "地质灾害消息推送";
                setThresholdContent(corrosion);
                initializeBizMsgSends(lastDate, geologicHazard, true, code);
            } else if ("RA_MAIN_THREATS_DSFPH".equals(mainThreats)) {//第三方破坏
                super.roleList = damage.getRoleIdList();
                this.createRemark = "第三方破坏消息推送";
                setThresholdContent(corrosion.setThirdDamage(true));
                initializeBizMsgSends(lastDate, damage, true, code);
            }
        }

    }

    /**
     * 阴保有效性评价
     */
    private enum CathodicProtectionEvaluationEnum{
        DEFICIENCY("欠保护"),
        EXCESSIVE("过保护");

        private String value;

        private CathodicProtectionEvaluationEnum(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

    }

    /**
     * 交流干扰程度/直流干扰程度
     */
    public enum CurrentFerenceEnum{
        BIG("强"),//强
        ORDINARY("中"),//中
        LITTLE("弱"); //弱
        private String value;

        private CurrentFerenceEnum(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

    /**
     * 交流/直流干扰消息推送
     * @param acDcMior
     * @param lineId
     * @param pipeLineName
     * @param fromReferenceId
     * @param toReferenceId
     * @param beginMile
     * @param endMile
     * @param acDcMiorObj
     */
    private void acDcMior(BizMsgThresholdVO acDcMior, String lineId, String pipeLineName, String fromReferenceId,
                          String toReferenceId, Long beginMile, Long endMile, Map acDcMiorObj) {
        boolean isAc = false, isDc = false, isAcDc = false; //交流干扰、直流干扰以及直流交流共同干扰布尔值，默认为不选中
        if(acDcMiorObj.get("acInterference") != null) //业务阀值中设置的“交流干扰”值
            isAc = true;
        if(acDcMiorObj.get("dcInterference") != null)  //业务阀值中设置的“直流干扰”值
            isDc = true;
        if(acDcMiorObj.get("acDcInterference") != null) //业务阀值中设置的“直流交流共同干扰”值
            isAcDc = true;
        if(isAc || isDc || isAcDc) {
            List<AcDcMiorRecordDTO> acDcMiorRecordDTOs = super.bizMsgThresholdService.queryRiskAcDcMiorInfo(lineId, beginMile, endMile);

            long acCount = 0, dcCount = 0, acDcCount = 0; //分别对应为：交流干扰个数、直流干扰个数以及直流交流共同干扰类型
            for (int i = 0; i < acDcMiorRecordDTOs.size(); i++) {
                AcDcMiorRecordDTO miorRecordDTO = acDcMiorRecordDTOs.get(i);
                String result = miorRecordDTO.getResult(); //result格式为：干扰程度-类型,干扰程度-类型
                if(StringUtils.isNotEmpty(result)) {
                    String[] acDcAlls = result.split(",");
                    if(acDcAlls.length == 2) {
                        String s1 =  acDcAlls[0];
                        String s2 =  acDcAlls[1];
                        if(StringUtils.isNotEmpty(s1) && StringUtils.isNotEmpty(s2)) {
                            String acDcMiors1[] = s1.split("-");
                            String acDcMiors2[] = s2.split("-");
                            if(acDcMiors1.length == 2 && acDcMiors2.length == 2) {
                                Map<String, String> map = new HashMap<String, String>();
                                map.put(acDcMiors1[0], acDcMiors1[1]);
                                map.put(acDcMiors2[0], acDcMiors2[1]);

                                String acMiorRecord =  map.get(AC);
                                String dcMiorRecord =  map.get(DC);
                                Table<String, String ,String> table = initAcDcMiorTable();
                                String value = table.get(acMiorRecord, dcMiorRecord);
                                if(StringUtils.isNotEmpty(value)) {
                                    if(AC.equals(value)){
                                        acCount++;
                                    } else if(DC.equals(value)){
                                        dcCount++;
                                    } else if(AC_DC.equals(value)){
                                        acDcCount++;
                                    }
                                }
                                if((acCount + dcCount + acDcCount) > 0) {
                                    Corrosion corrosion = new Corrosion();
                                    List<String> strs = new ArrayList<String>();
                                    if(acCount > 0 && isAc) {
                                        strs.add("【"+ acCount +"】处交流干扰");
                                    }
                                    if(dcCount > 0 && isDc) {
                                        strs.add("【"+ dcCount +"】处直流干扰");
                                    }
                                    if(acDcCount > 0 && isAcDc && isAcDc) {
                                        strs.add("【"+ acDcCount +"】处直流交流共同干扰");
                                    }
                                    String allStr = Joiner.on(",").join(strs);
                                    setThresholdContent(corrosion.setPipeLineId(lineId).setPipeLineName(pipeLineName) .setFromId(fromReferenceId)
                                            .setToId(toReferenceId).setAcDcStr(allStr).setOverUnderStr(""));

                                    super.roleList = acDcMior.getRoleIdList();
                                    this.createRemark = "交流直流干扰消息推送";
                                    initializeBizMsgSends(null, acDcMior, false);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 过保护欠保护消息推送
     * @param overUnderProtection
     * @param lineId
     * @param pipeLineName
     * @param fromReferenceId
     * @param toReferenceId
     * @param beginMile
     * @param endMile
     * @param overUnderMap
     */
    private void overUnderProtection(BizMsgThresholdVO overUnderProtection, String lineId, String pipeLineName, String fromReferenceId,
                                     String toReferenceId, Long beginMile, Long endMile, Map overUnderMap) {
        boolean isOver = false, isUnder = false; //过保护欠保护布尔值，默认为不选中
        if(overUnderMap.get("overProtection") != null) { //业务阀值中设置的“过保护”值
            isOver = true;
        }
        if(overUnderMap.get("underProtection") != null) { //业务阀值中设置的“欠保护”值
            isUnder = true;
        }
        if(isOver || isUnder) {
            List<String> overUnderStrs = new ArrayList<String>();
            if(isOver) {
                overUnderStrs.add(CathodicProtectionEvaluationEnum.DEFICIENCY.getValue());
            }
            if(isUnder) {
                overUnderStrs.add(CathodicProtectionEvaluationEnum.EXCESSIVE.getValue());
            }
            if(!overUnderStrs.isEmpty()) {
                List<ExinspCptestStationDTO> exinspCptestStationDTOs = super.bizMsgThresholdService.queryRiskExinspCptestStation(lineId, beginMile, endMile);
                long overUnderCount = 0; //测试桩检测信息计数
                for (int i = 0; i < exinspCptestStationDTOs.size(); i++) {
                    ExinspCptestStationDTO cptestStationDTO = exinspCptestStationDTOs.get(i);
                    String protectingState = cptestStationDTO.getProtectingState(); //阴保有效性评价
                    if (CathodicProtectionEvaluationEnum.DEFICIENCY.toString().equals(protectingState) && isUnder) {//欠保护
                        overUnderCount++;
                    }
                    if (CathodicProtectionEvaluationEnum.EXCESSIVE.toString().equals(protectingState) && isOver) { //过保护
                        overUnderCount++;
                    }
                }
                if (overUnderCount != 0) {
                    Corrosion corrosion = new Corrosion();
                    String str = corrosion.getOverUnderStr();
                    setThresholdContent(corrosion.setPipeLineId(lineId).setPipeLineName(pipeLineName) .setFromId(fromReferenceId)
                            .setToId(toReferenceId).setOverUnderStr(MessageFormat.format(str, overUnderCount, Joiner.on("/").join(overUnderStrs))));
                    super.roleList = overUnderProtection.getRoleIdList();
                    this.createRemark = "立即修复管体缺陷消息推送";
                    initializeBizMsgSends(null, overUnderProtection, false);
                }
            }
        }
    }

    /**
     * 当风险评价中“腐蚀”为主要威胁因素的时候，所需的实体类
     */
    private class Corrosion {
        private String pipeLineId; //管道id
        private String pipeLineName; //管道名称
        private String fromId; //起点位置id
        private String toId; //终点位置id
        private String overUnderStr = "【{0}】处阴极保护为{1}状态"; // 过保护欠保护消息推送所需的字符串
        private String acDcStr = ""; //交流/直流干扰所需的字符串

        private boolean isThirdDamage = false; //是否为第三方破坏

        public String getPipeLineId() {
            return pipeLineId;
        }

        public Corrosion setPipeLineId(String pipeLineId) {
            this.pipeLineId = pipeLineId;
            return this;
        }

        public String getPipeLineName() {
            return pipeLineName;
        }

        public Corrosion setPipeLineName(String pipeLineName) {
            this.pipeLineName = pipeLineName;
            return this;
        }

        public String getFromId() {
            return fromId;
        }

        public Corrosion setFromId(String fromId) {
            this.fromId = fromId;
            return this;
        }

        public String getToId() {
            return toId;
        }

        public Corrosion setToId(String toId) {
            this.toId = toId;
            return this;
        }

        public String getOverUnderStr() {
            return overUnderStr;
        }

        public Corrosion setOverUnderStr(String overUnderStr) {
            this.overUnderStr = overUnderStr;
            return this;
        }

        public String getAcDcStr() {
            return acDcStr;
        }

        public Corrosion setAcDcStr(String acDcStr) {
            this.acDcStr = acDcStr;
            return this;
        }

        public boolean isThirdDamage() {
            return isThirdDamage;
        }

        public Corrosion setThirdDamage(boolean thirdDamage) {
            isThirdDamage = thirdDamage;
            return this;
        }
    }

    //交直流干扰所需的字符串
    private static final String AC = "ac";
    private static final String DC = "dc";
    private static final String AC_DC = "acdc";

    /**
     * 初始化交流/直流干扰程度对应关系表
     * @return
     */
    private Table<String, String ,String> initAcDcMiorTable() {
        Table<String, String ,String> table = HashBasedTable.create();
        String big = CurrentFerenceEnum.BIG.toString();
        String ordinary = CurrentFerenceEnum.ORDINARY.toString();
        String little = CurrentFerenceEnum.LITTLE.toString();
        table.put(little, little, "");
        table.put(little, ordinary, DC);
        table.put(little, big, DC);

        table.put(ordinary, little, AC);
        table.put(ordinary, ordinary, AC_DC);
        table.put(ordinary, big, AC_DC);

        table.put(big, little, AC);
        table.put(big, ordinary, AC_DC);
        table.put(big, big, AC_DC);
        return table;
    }


    /**
     * @param args 参数说明：
     * <pre>
     * args[0]：为最后一次的时间，类型为：{@link Date} <br />
     * args[1]：为业务阀值实体信息，类型为：{@link BizMsgThresholdVO}<br />
     * args[2]：为是否需要校验，类型为：{@link Boolean}<br />
     * args[3]：当args[2]为true时可以取值，类型为：{@link String}
     * </pre>
     */
    @Override
    public void initializeBizMsgSends(Object ... args) {
        boolean isVali = (Boolean) args[2];
        BizMsgThresholdVO bizMsgThresholdVO = (BizMsgThresholdVO) args[1];

        String thresholdId = bizMsgThresholdVO.getId();
        String msgContent = bizMsgThresholdVO.getMsgContent();
        BizMsgUser bizMsgUser = new BizMsgUser();
        bizMsgUser.setCreateRemark(this.createRemark);

        if (StringUtils.isNotEmpty(msgContent))//消息内容不为空的时候设置到消息推送实体类
            bizMsgUser.setMsgContent(msgContent);
        if (StringUtils.isNotEmpty(super.thresholdContent)) { //设置阀值内容
            bizMsgUser.setThresholdContent(super.thresholdContent);
        }
        if(args[0] != null) {
            bizMsgUser.setUpdateDate((Date) args[0]);
            bizMsgUser.setUpdateRemark(this.updateRemark);
        }
        if (super.roleList != null && !super.roleList.isEmpty()) {
            Set<String> userIds = super.bizMsgThresholdService.queryUseIdByRoleIds(super.roleList); //用户id集合
            List<MsgQueue.BizMsgUserInfo> userInfos = new ArrayList<MsgQueue.BizMsgUserInfo>();
            for (String userId : userIds) {
                BizMsgUser msgUser = new BizMsgUser();
                BeanUtils.copyProperties(bizMsgUser, msgUser);
                msgUser.setCreateDate(new Date());
                msgUser.setUserId(userId);
                msgUser.setId(CommonUtil.getUUID());

                Map<String, Object> valiMap = new HashMap<String, Object>();
                if(isVali) {
                    String code = (String) args[3];
                    valiMap.put("userId", userId);
                    valiMap.put("bizMsgThresholdId", thresholdId);
                    valiMap.put("lastDate", args[0]);
                    valiMap.put("code", code);

                    msgUser.setUpdateRemark(code);
                }
                userInfos.add(msgQueue.new BizMsgUserInfo().setValid(isVali).setValiMap(valiMap).setBizMsgUser(msgUser).setPushMsgTemplate(this));
            }
            if(!userInfos.isEmpty()) {
                MsgQueue.MsgInfo msgInfo = msgQueue.new MsgInfo().setBizMsgThresholdId(thresholdId)
                        .setThresholdSet(bizMsgThresholdVO.getThresholdContent()).setRoleIds(super.roleList);
                msgQueue.addOperator(msgQueue.new PushMsgOperator(userInfos, msgInfo));
            }
        }
        super.roleList = null;
    }

    @Override
    public boolean valiIsPush(Map<String, Object> valiMap) {
        return super.bizMsgThresholdService.countRiskDefectMsg(valiMap);
    }

}